
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

unsigned int spi_send_data(unsigned char *pBuffer,int length)
{
	unsigned char wr_buf[1300],rd_buf[1300];
	int  status;
	struct spi_ioc_transfer xfer[2];
	int file,i;

	file = open("/dev/spidev1.1", O_RDWR);
	if (file<0) {
		printf("open xilinx v7 error\n");
		return 1;
	}
	memset(wr_buf, 0, sizeof(wr_buf));
	memset(rd_buf, 0, sizeof(rd_buf));
	memset(xfer,0,sizeof(xfer));

	for(i=0;i<length;i++)
	{
		wr_buf[i+2]=pBuffer[i];
	}

	wr_buf[0] = 0x5A;
	wr_buf[1] = 0x5A;

	wr_buf[length+2] = 0xA5;
	wr_buf[length+3] = 0xA5;
	xfer[0].tx_buf = (unsigned long) wr_buf;
	xfer[0].len = length+4;

	xfer[1].rx_buf = (unsigned long) rd_buf;
	xfer[1].len = 0;
	status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);

	close(file);
	return status;
}

